package com.example.carrierapp.base;

import com.example.carrierapp.net.ApiStrores;
import com.example.carrierapp.net.AppClient;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

import retrofit2.Retrofit;

/**
 * 基础Presenter，封装了与View层的关联、解除等方法
 */
public abstract class BasePresenter<V> {
    protected Retrofit retrofit = AppClient.getInstance().getRetrofit();
    protected ApiStrores apiStrores = retrofit.create(ApiStrores.class);

    protected Reference<V> mViewRef; //View接口类型的弱引用

    public void attachView(V view) {
        mViewRef = new WeakReference<V>(view); //与Veiw建立关联
    }

    protected V getView() {
        return mViewRef!=null?mViewRef.get():null;
    }

    //判断是否与View建立关联
    public boolean isViewAttached() {
        return mViewRef != null && mViewRef.get() != null;
    }

    //解除关联
    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }
    public abstract void start();
}
